package com.china08.yxyapi.api.manage.sch.platform.xiaoben.auth;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonAuth;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonAuthRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class UserAuthAddController extends YxyApiBaseController {

    @Autowired
    YxyLessonsSchoolLessonAuthRepository authRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/sch/xiaoben/auth/user/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String schoolId = getProfile(request, response).getSchoolId();
        String userId = reqModel.userId;
        String userName = reqModel.userName;
        String studentName = reqModel.studentName;
        String classId = reqModel.classId;
        String className = reqModel.className;
        String guardianName = reqModel.guardianName;
        List<String> lessonIds = reqModel.lessonIds;

        for (String lessonId : lessonIds) {
            YxyLessonsSchoolLessonAuth auth = new YxyLessonsSchoolLessonAuth();
            auth.setSchoolId(schoolId);
            auth.setUserId(userId);
            auth.setUserNick(studentName);
            auth.setUserName(userName);
            auth.setLessonId(lessonId);
            auth.setClassId(classId);
            auth.setClassName(className);
            auth.setGuardianName(guardianName);
            auth.setGuardianId("1");
            auth.setStatus(1);
            authRepository.save(auth);
        }
    }

    public static class ReqModel extends YxyApiPageReqModel {
        public String userId;
        public String userName;
        public String studentName;
        public String classId;
        public String className;
        public String guardianName;
        public List<String> lessonIds;

        @Override
        public void autoCheck() {
            Assert.isTrue(lessonIds.size() > 0, "请选择课程");
        }
    }

    public static class RespModel extends YxyApiRespModel {
    }
}
